#pragma once
#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>

class InetAddr
{
private:
    void GetAddress(std::string* ip, uint16_t* port)
    {
        *ip = inet_ntoa(_addr.sin_addr);    //四字节ip转化为字符串风格ip 并转化为主机序列
        *port = ntohs(_addr.sin_port);      //端口号网络序列转换为主机序列
    }
public:
    InetAddr()
    {}
    InetAddr(struct sockaddr_in addr)
    :_addr(addr)
    {
        GetAddress(&_ip, &_port);
    }
    InetAddr(std::string ip, uint16_t port)
    :_ip(ip)
    ,_port(port)
    {}

    std::string Ip()
    {
        return _ip;
    }
    uint16_t Port()
    {
        return _port;
    }
private:
    std::string _ip;
    uint16_t _port;
    sockaddr_in _addr;
};